var SPSdk = (function () {
    var DOMAIN = "http://youxi11-wap.stg2.24cp.com/";//test
   // var DOMAIN = "http://gameapp.wanlitong.com/";
    var cSign = "http://ucenter.layabox.com/ClientSign/GetSign";
    function SPSdk() {
        this.appId = "";
        this.appKey = "";
        this.loginKey = "";
        this.openId = "";
        this.openKey = "";
        this.payKey = "";
        this.spFname = "";
        this.spId = "";
        this.userId = "";
        this.token = "";
        this.userName = "";
        this.timestamp = "";
        this.sign = "";
        this.queryParams=null;
    }
    var __proto = SPSdk.prototype;
    __proto.init = function (param, callback) {
        console.log("平安游戏初始化：" + JSON.stringify(param));
        var m_this = this;
        m_this.appId = param.appId;
        m_this.appKey = param.appKey;
        m_this.loginKey = param.loginKey;
        m_this.openId = param.openId;
        m_this.openKey = param.openKey;
        m_this.payKey = param.payKey;
        m_this.spFname = param.spFname;
        m_this.spId = param.spId;
        m_this.queryParams = param.queryParams;

        if (m_this.queryParams&&m_this.queryParams["appkey"]) {
            //LayaCommon.closeWnd("payxPopupLoginView");
            m_this.userId =param.queryParams.userId;
            m_this.userName = decodeURIComponent(param.queryParams.userName);
            m_this.token = decodeURIComponent(param.queryParams.token);
            m_this.timestamp = decodeURIComponent(param.queryParams.timestamp);
            m_this.sign = param.queryParams.sign;
            callback && callback({"result": 0, "desc": "OK"});
        }else{
            //LayaCommon.openUrl("payxPopupLoginView", DOMAIN + "?act=user&st=login&from=wap&appkey=" + param.appId);
            window.location.href = DOMAIN + "?act=user&st=login&from=wap&appkey=" + param.appId;
        }
    };

    __proto.login = function (param, callback) {
        console.log("平安游戏登录：" + JSON.stringify(param));
        var m_this = this;
        //为了调试暂时写固定SLF
        //m_this.appKey = "bfa8105d455dcd8f8e70d84442a0afd9";
        //m_this.appId = "d4647010cf9f3823b97798ada53913a3";
        var source =
            m_this.appKey +
            location.pathname.replace(".", "").replace("/", "") +window.location.search.replace("=","")+
            "appkey" + m_this.appId +
            "timestamp" + m_this.timestamp.replace(" ", "") +
            "token" + m_this.token +
            "userId" + m_this.userId +
            "userName" + m_this.userName;
        source = encodeURIComponent(source);
        var url = cSign+"?resource="+encodeURI(source)+"&signtype=SHA1&access_token="+m_this.token;
        LayaCommon.getJson(url,function(res){
            if(res.ret ==0)
            {
                var value = res.data[0];
                if (value.toUpperCase() == m_this.sign) {
                    callback && callback({"result":0,"spuid":m_this.userId,"username":m_this.userName});
                }else
                {
                    callback && callback({"result":-2,"desc":"登录失败"});
                }
            }
        })
    };
    __proto.pay = function (param, callback) {
        console.log("平安游戏支付：" + JSON.stringify(param));
        var m_this = this;
        top.location.href =
            DOMAIN +
            "?act=payment" +
            "&from=wap" +
            "&appkey=" + m_this.appId +
            "&token=" + encodeURIComponent(m_this.token) +
            "&tradeName=" + param.data.goodsName +
            "&gameCoinAmount=" + Math.round(param.data.amount / 10.00) +
            "&serverId=1" +
            "&gameOrderId=" + param.data.orderId;
        ;
    };
    return SPSdk;
})();